package com.freeter.modules.controller;


import com.freeter.modules.gjs.dto.ReCalcDataObj;
import com.freeter.modules.gjs.entity.*;
import com.freeter.modules.gjs.service.*;
import com.freeter.modules.gjs.service.impl.BusinessLogicProcessor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class APIController {

    private static final Logger logger = LogManager.getLogger(APIController.class);

    @Autowired
    private BusinessLogicProcessor  bizProcessor;

    /**
     * 损坏报告
     * 读取并解析，status 为 Pending 时，查找有权限的用户，生成确认链接并邮件通知
     * 错误重试机制：如有异常递归重试
     * @param params
     * @return
     */
    @PostMapping("/damage_report")
    public ResponseEntity<?> damageReport(@RequestBody Map<String,Object> params) {
        logger.info("damage_report, param:{}",params);
        bizProcessor.processDamageReport(params);
        return ResponseEntity.ok("ok");
    }

    /**
     * 按 status 字段分别处理 Pending/Approved/Rejected
     *  - Pending：查找有权限的用户，生成审批链接并邮件通知
     *  - Approved/Rejected：通知申请人结果
     *  - 错误重试机制：如有异常递归重试
     * @param params
     * @return
     */
    @PostMapping("/transfer_request")
    public ResponseEntity<?> transferRequest(@RequestBody Map<String,Object> params) {
        logger.info("transfer_request, param:{}",params);
        bizProcessor.processTransferRequest(params);
        return ResponseEntity.ok("ok");
    }

    /**
     * 领用入库
     * @param params
     * @return
     */
    @PostMapping("/usedInStock")
    public ResponseEntity<?> usedInStock(@RequestBody Map<String,Object> params) {
        logger.info("usedInStock, param:{}",params);
        bizProcessor.processUseTransaction(params.get("id").toString());
        return ResponseEntity.ok("ok");
    }

    /**
     * gr 入库
     * @param params
     * @return
     */
    @PostMapping("/grInStock")
    public ResponseEntity<?> grInStock(@RequestBody Map<String,Object> params) {
        logger.info("grInStock, param:{}",params);
        bizProcessor.processGrTransaction(params.get("ID").toString());
        return ResponseEntity.ok("ok");
    }

    /**
     * 报损
     * @param params
     * @return
     */
    @PostMapping("/damage")
    public ResponseEntity<?> damage(@RequestBody Map<String,Object> params) {
        logger.info("damage, param:{}",params);
        bizProcessor.processDamageTransaction(Long.valueOf(params.get("id").toString()));
        return ResponseEntity.ok("ok");
    }

}